import path from "path"
import fs from "fs"
import { readMDFile, parseTime } from "./markdown";
import { CSDNAutoDeployBlog } from "./platforms/csdn"
// 本地远程文件夹路径
let mdPath = 'C:\\Users\\duan\\Documents\\WPS Cloud Files\\279367518\\博客\\';
// 返回的是字符串数组，内容是目录里面的文件名数组
let allBlogNameList = fs.readdirSync(path.resolve(mdPath), { encoding: 'utf-8' })
console.log('allBlogNameList :>> ', allBlogNameList);

// 保存已发布博客
let savedString: string = fs.readFileSync(path.resolve('./src/data.json'), { encoding: 'utf-8' })
let savedData: blogData = JSON.parse(savedString)
// console.log('savedData :>> ', savedData);
let publishedBlogNameList = Object.keys(savedData)
console.log('publishedBlogNameList :>> ', publishedBlogNameList);
// 得到没有发布过的博客文件名数组
let planBlogList: Array<string> = []
allBlogNameList.forEach((item: string) => {
    if (publishedBlogNameList.findIndex(i => i == item) < 0) {
        planBlogList.push(item)
    }
})
console.log('planBlogList :>> ', planBlogList);

// TODO 开始发布
let data = readMDFile(mdPath + planBlogList[0])
console.log('data :>> ', data);

let cookies = 'uuid_tt_dd=10_19289789170-1591672702319-785094; dc_session_id=10_1650264369844.737686; c_pref=default; c_ref=default; c_first_ref=cn.bing.com; c_first_page=https%3A//blog.csdn.net/zdhsoft/article/details/117907367; c_segment=6; c_page_id=default; dc_tos=raiwex; log_Id_pv=1175; hide_login=1; Hm_lvt_6bcd52f51e9b3dce32bec4a3997715ac=1650264730; Hm_lpvt_6bcd52f51e9b3dce32bec4a3997715ac=1650264730; Hm_up_6bcd52f51e9b3dce32bec4a3997715ac=%7B%22uid_%22%3A%7B%22value%22%3A%22cuipp0509%22%2C%22scope%22%3A1%7D%2C%22islogin%22%3A%7B%22value%22%3A%221%22%2C%22scope%22%3A1%7D%2C%22isonline%22%3A%7B%22value%22%3A%221%22%2C%22scope%22%3A1%7D%2C%22isvip%22%3A%7B%22value%22%3A%220%22%2C%22scope%22%3A1%7D%7D; Hm_ct_6bcd52f51e9b3dce32bec4a3997715ac=6525*1*10_19289789170-1591672702319-785094!5744*1*cuipp0509; dc_sid=c048c0187af82eb9ea1172f860527e59; log_Id_view=2981; SESSION=c481b8e1-7a9e-4265-a462-c47aaf6bc171; ssxmod_itna=QqUx9D0DcDgDR07qGHD8D2QXKPROfW/WWACiAWDBMf84iNDnD8x7YDvIILD0xO+2hteWchRpbKW2qed5lKARGhdf+884GLDmKDyKB74XoD4SKGwD0eG+DD4DWDmWHDnxAQDjxGp9uXwV=Dm4GW8qDmDGA9tqDgDYQDGdI6D7QDIL63iDh9iCdK6wbSKD2dUA=DjubD/bhagebF1PhxagQDzMFDtqNMSLd3x0PBldXxv0h3WoO6l4DRUPGM0lKDAf6diY7eD7v9BfdilKDlDO+sFg+xUG5KnCDus8Z3fBD5YDA3Us1zQawuxAqDDWOdqU7D4D; UN=cuipp0509; _ga=GA1.2.646447570.1597217438; __gads=ID; aliyun_webUmidToken=T2gAamfKoBgc-1TL0gmU28BmEafqLacJMzjjZp6vj4H070g4j5qv8r5x9wfIEoUJs-w; p_uid=U010000; ssxmod_itna2=QqUx9D0DcDgDR07qGHD8D2QXKPROfW/WWACiKD8dZ46PGX9xKGaUYiAL7U/zx82x6CkDzOA=DBDWBXwPVaxg6HgC8uiak9CiZAiA557wGr0GdMnGtjDLop8U2qfDCQxR2AEA=2l5o4xen3A98bjxzEo0FSAL3RrnYejj88QYEg2KueUjjSbNuWYPU0AQs3=DMMnl3lvddufv4Z27P=NOH6F4rpH17id+3T3hq90E3TA+2L7/Y3dlmlRVAhbThct80tffkMa621df/z60i9ydb3223nLuucCgfMf+dlOII3dMa4ARZzS2hvOOeVPKDIfvn3+c3vc39cdUctZ2v9pKEl=A0toP26Ef2PoXgYY3bXceBmKdgitEbQfNiKN87GpmwSEQ6gDTarT1N8f6Smd5mAC=GdW3bzxPAv96E5pPNtjQxLWDG29rHC8GK0ubRXU8Qz439f=7i18x=7G50R7GiP3MridpUWoD08DijxhD4exG1q5YD===; UserName=cuipp0509; UserInfo=216bbc95f7bd4cdba3607f158e366010; UserToken=216bbc95f7bd4cdba3607f158e366010; UserNick=%E5%96%9C%E6%AC%A2%E8%B7%AF%E9%A3%9E%E7%9A%84%E9%93%B6%E6%A1%91; AU=253; BT=1650264664475; log_Id_click=524; c_dl_prid=-; c_dl_rid=1650261044574_235219; c_dl_fref=https://blog.csdn.net/weixin_40438455/article/details/103779838; c_dl_fpage=/download/weixin_38601364/13199044;'

async function AutoDeploy() {

    let url  = await CSDNAutoDeployBlog(cookies, data)

    if (url) {
        savedData[planBlogList[0]] = {
            publishedUrl: url,
            publishedTime: parseTime(new Date().getTime()),
            ...data.titleObj
        }
    }
    
    // 添加新发布的数据写回文件
    savedString = JSON.stringify(savedData)
    fs.writeFileSync(path.resolve('./src/data.json'), savedString, { encoding: 'utf-8' })
}

AutoDeploy()
